var gbl_delay_sec = 3;
var gbl_fade_sec = 1;

window.onload = function() {
	init_gallery('gallery');
}

function init_gallery(id) {
	var gallery = document.getElementById(id);
	var imgs = new Array;
	var node;
	for (node = gallery.firstChild; node; node = node.nextSibling) {
		if (node.nodeType == Node.ELEMENT_NODE) {
			//node.style.position = 'absolute';
			//node.style.top = 0;
			node.style.zIndex = 0;
			set_opacity(node, 0);
			node.style.visibility = 'visible';
			imgs.push(node);
		}
	}

	set_opacity(imgs[0], 100);
	window.setTimeout(function() { slideshow_fade(imgs, 1, 0) }, gbl_delay_sec * 1000);
}

function slideshow_fade(imgs, i, opacity) {
	if (opacity < 100) {
		opacity += 100 / (30 * gbl_fade_sec);
		set_opacity(imgs[i], opacity);
		window.setTimeout(function() { slideshow_fade(imgs, i, opacity) }, 30);
	} else {
		var prev = (i > 0) ? imgs[i - 1] : imgs[imgs.length - 1];
		set_opacity(prev, 0);
		/* Next image on top of the previous one */
		imgs[i].style.zIndex = 0;
		i = (i + 1) % imgs.length;
		imgs[i].style.zIndex = 1;
		window.setTimeout(function() { slideshow_fade(imgs, i, 0) }, gbl_delay_sec * 1000);
	}
}

function set_opacity(obj, opacity) {
	opacity = (opacity == 100) ? 99.999 : opacity;
	obj.style.filter = 'alpha(opacity:' + opacity + ')'; /* IE */
	opacity /= 100;
	obj.style.KHTMLOpacity = opacity; /* Safari (< v1.2), Konqueror */
	obj.style.MozOpacity = opacity; /* Older Mozilla and Firefox */
	obj.style.opacity = opacity; /* Safari (v1.2+), newer Firefox and Mozilla, CSS3 */
}
